#!/usr/bin/env python
# encoding: utf-8
"""
cache.py

Created by AlanYang on 2011-06-20.
Copyright (c) 2011 __MyCompanyName__. All rights reserved.
"""
from functools import wraps
from config import memcacheclients

class Cache(object):
    def __init__(self,contail):
        if not getattr(contail, 'memcache',None):
            import memcache
            contail.memcache = memcache.Client(memcacheclients)
        self.memcache = contail.memcache
        self.expire   = 10
        
    def caching(self,expire=None,prefix='%s'):
        def __magic__(func):
            @wraps(func)
            def __do__(*args,**kv):
                key = prefic%func.__name__
                value = self.memcache.get(key)
                if not value:
                    value = func(*args,**kv)
                    expire = expire or self.expire
                    self.memcache.set(key,value,expire)
                return value
            return __do__
        return __magic__
            


